import java.io.IOException;
import java.util.*;
public class CalczOperator  {
    /** constant for addition symbol */
    private final char ADD = '+';
    /** constant for subtraction symbol */
    private final char SUBTRACT = '-';
    /** constant for multiplication symbol */
    private final char MULTIPLY = 'x';
    /** constant for division symbol */
    private final char DIVIDE = '/';
    /** constant for MOD symbol */
    private final char MOD = '%';
    /** the stack */
    Stack<Integer> stack=new Stack<Integer>();
    Stack<Character> stack1=new Stack<Character>();
     int operate(String str)throws IOException {
        int op1,op2,result=0;
        int i=0;
        String string;
        StringTokenizer fenxi=new StringTokenizer(str);
        while (fenxi.hasMoreTokens())
        {
            string = fenxi.nextToken();
            //如果是运算符，调用isOperator
            if (isOperator(string))
            {
                char c[]=string.toCharArray();
                stack1.push(c[0]);
            }
            else{
                //如果是操作数
                if(string.matches("[0-9]+")) {
                    i++;
                    int number=Integer.parseInt(string);
                    stack.push(number);
                    if(i==2){
                        op1=stack.pop();
                        op2=stack.pop();
                        result=result=evalSingleOp(stack1.pop(),op2,op1);
                    }
                }
            }
        }
        return result;
    }
  private int evalSingleOp (char operation, int op1, int op2)
        {
        int result = 0;

        switch (operation)
        {
        case ADD:
        result = op1 + op2;
        break;
        case SUBTRACT:
        result = op1 - op2;
        break;
        case MULTIPLY :
        result = op1 * op2;
        break;
        case DIVIDE:
        result = op1 / op2;
        break;
        case MOD :
        result = op1 % op2;
        break;
        }

        return result;
   }
  private boolean isOperator(String str){
       return (str.equals("+")||str.equals("x")||str.equals("/")||str.equals("-")||str.equals("%"));

  }
}
